package wk.demo.fragments;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.luoye.bzijkplayer.R;

import org.litepal.LitePal;

import java.util.List;

import wk.demo.activities.VideoActivity;
import wk.demo.adapter.HistoryAdapter;
import wk.demo.bean.HistoryReadBean;
import wk.demo.bean.MarginDecoration;
import wk.demo.widget.ItemRemoveRecyclerView;


public class RecentMediaListFragment extends Fragment  {
    private ItemRemoveRecyclerView recyclerViewSwipe;
    private HistoryAdapter adapter ;

    public static RecentMediaListFragment newInstance() {
        return new RecentMediaListFragment();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.recyclerview_list_lay, container, false);
        recyclerViewSwipe =  rootView.findViewById(R.id.recyclerViewSwipe);
        recyclerViewSwipe.setHasFixedSize(true);
        recyclerViewSwipe.setLayoutManager(new GridLayoutManager( getActivity(), 1));
        recyclerViewSwipe.addItemDecoration(new MarginDecoration(getActivity()));

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        List<HistoryReadBean> datas = LitePal.findAll(HistoryReadBean.class);
        adapter= new HistoryAdapter( getActivity() ,datas) ;
        recyclerViewSwipe.setAdapter(adapter);
        recyclerViewSwipe.setOnItemClickListener(new ItemRemoveRecyclerView.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                HistoryReadBean bean = datas.get(position) ;
                VideoActivity.intentTo(getActivity() , bean.getFilePaht() );
            }

            @Override
            public void onDeleteClick(int position) {
                HistoryReadBean bean = datas.get(position) ;
                bean.delete() ;// 从数据库删除
                adapter.removeItem(position);
            }
        });
    }





}
